package cn.hnu.qq.frame;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.EmptyBorder;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class FileWaitingDialog extends JDialog implements ActionListener{

	private final JPanel contentPanel = new JPanel(){
		@Override
		protected void paintComponent(Graphics g) {
			Image image = null;
			try {
				image = ImageIO.read(new File("src/main/java/image/02.jpg"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			};
					
			g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

		}
	};
	private JLabel info;
	private JProgressBar progressBar;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			FileWaitingDialog dialog = new FileWaitingDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public FileWaitingDialog() {
		init();
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}

	public FileWaitingDialog(Dialog owner, boolean modal) {
		super(owner, modal);
		init();
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}

	public FileWaitingDialog(Frame owner, boolean modal) {
		super(owner, modal);
		init();
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}

	public FileWaitingDialog(Frame owner, String title) {
		super(owner, title);
		init();
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}

	public FileWaitingDialog(Window owner) {
		super(owner);
		init();
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}

	private void init() {
		try {
			setIconImage(ImageIO.read(new File("src/main/java/image/xiaohui.jpg")));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		setBounds(100, 100, 365, 242);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		{
			info = new JLabel("12312312312312312312323123123");
			contentPanel.add(info, "8, 6, fill, default");
		}
		progressBar = new JProgressBar();
		contentPanel.add(progressBar, "8, 8");

		
		{
			JLabel label = new JLabel("进度：");
			contentPanel.add(label, "4, 8");
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
				cancelButton.addActionListener(this);
			}
		}
	}
	public JProgressBar getProgressBar(){
		return this.progressBar;
	}
	public void setInfo(String info){
		this.info.setText(info);
		this.info.validate();
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("Cancel")){
			if(progressBar.getValue() != progressBar.getMaximum()){
				JOptionPane.showMessageDialog(this, "文件接收尚未完成，转至后台接收");
			}
			this.dispose();
		}
	}
}
